﻿/*变量定义部分*/
var grid;
//数据源的定义
var reader = new Ext.data.JsonReader(
        {
            totalProperty: "totalSize", autoLoad: true, fields: [{ name: 'Id' },
                    { name: 'Pid' }, { name: 'Name' }, { name: 'Type' }, { name: 'RealName' }, { name: 'Size' },
                    { name: 'CreatTime' }, { name: 'OwnerId' }, { name: 'OrderNo' }
                ]
        });
var store = new Ext.data.Store(
        {
            scopte: this, url: '/File.mvc/Find', reader: reader, sortInfo: {
                field: 'Id', direction: "ASC"
            }
        });
var fromURL;//记录动作发生的目录
//选择行
var SelectDataItems = new Array();
var sm = new Ext.grid.RowSelectionModel({
    singleSelect: false,
    hidden:true
});

//提交服务器的文件类型
function sendFiles(FileName, FileType) {
    this.fileName = FileName;
    this.fileType = FileType;
}

//Grid用的类模板
var cm = new Ext.grid.ColumnModel([sm,new Ext.grid.RowNumberer(),
    {
        header: "名称",
        dataIndex: "Name",
        width: 250
    }, {
        header: "类别",
        dataIndex: "Type",
        width: 50
    }, {
        header: "大小",
        dataIndex: "Size",
        renderer: DataSizeChange,
        width: 80
    }, {
        header: "创建时间",
        dataIndex: "CreatTime",
        renderer: DateParse,
        width: 100
    }
]);

/*内部函数部分*/
function DateParse(val) { return eval("new " + val.substr(1, val.length - 2)).format('Y-m-d H:i:s') }
function DataSizeChange(val) {
    if (val > 0) {
        val = Ext.util.Format.fileSize(val);
    }
    else {
        val = "";
    }
    return val
}

function rowdbClick(grid, index, e) {
    var type = grid.getStore().getAt(index).data.Type
    if (type == "Folder") {
        EnterPage(grid.getStore().getAt(index).data.FullName);
    }
    else if (type == ".txt" || type == ".cs" || type == ".css" || type == ".xml" || type == ".html" || type == ".htm" || type == ".js" || type == ".aspx" || type == ".cs") {
        var txt = new Ext.form.TextArea({ width: 400,
            height: 350
        });
        var req;
        Ext.Ajax.request({
            url: "fileManager.ashx",
            method: "Post",
            params: { Path: grid.getStore().getAt(index).data.FullName, Type: "Open" },
            callback: function (options, success, response) { if (success) { txt.setValue(response.responseText); } },
            failure: function () { Ext.MessageBox.show("对不起,系统出现错误请重试"); }
        });
        txt.setValue(req);
        var win = new Ext.Window({
            width: 500,
            height: 400,
            buttonAlign: "center",
            items: [txt]
        });
        win.show();
    }
}
function open_rowcontextmenu(grid, rowIndex, e) {
    e.preventDefault();
    var menus = new Ext.menu.Menu
        ([
            {
                xtype: "button", text: "打开", pressed: true,
                handler: function () {
                    openFile(grid.getStore().getAt(rowIndex).data.FullName, grid.getStore().getAt(rowIndex).data.Type);

                    grid.getBottomToolbar().items.item(0).setValue(grid.getStore().getAt(rowIndex).data.FullName);
                }
            },
            {
                xtype: "button", text: "删除", pressed: true,
                handler: function () {
                    Ext.MessageBox.confirm("请确认", "是否真的要删除指定的内容",
                            function (button, text) {
                                if (button == "yes") {
                                    DelFile(grid.getStore().getAt(rowIndex).data.Name, grid.getStore().getAt(rowIndex).data.Type);
                                }
                            });
                }
            }
        ]);
    menus.showAt(e.getPoint());
}

function openFile(path, type) {
    if (type == "Folder") {
        EnterPage(path);
    }
    else if (type == ".txt" || type == ".cs" || type == ".css" || type == ".xml" || type == ".html" || type == ".htm" || type == ".js" || type == ".aspx" || type == ".cs") {
        var txt = new Ext.form.TextArea({ width: 400,
            height: 350
        });
        var req;
        Ext.Ajax.request({
            url: "fileManager.ashx",
            method: "Post",
            params: { Path: path, Type: "Open" },
            callback: function (options, success, response) { if (success) { txt.setValue(response.responseText); } },
            failure: function () { Ext.MessageBox.show("对不起系统出现错误请重试"); }
        });
        txt.setValue(req);
        var win = new Ext.Window({
            width: 500,
            height: 400,
            buttonAlign: "center",
            items: [txt]
        });
        win.show();
    }
}
function DelFile(path, type) {
    Ext.Ajax.request({
        url: "FileManager.ashx",
        method: "Post",
        params: { Path: path + "☆" + type, Type: "Del", Root: document.getElementById("Now_Path").value },
        callback: function (options, success, response) {
            if (success) {
                Ext.MessageBox.alert("提示信息", path + "成功删除");
                Rerender(document.getElementById("Now_Path").value);
            }
            else {
                Ext.MessageBox.alert("提示信息", path + "删除错误(可能是权限不够请联系管理员)");
            }
        }
    });
}

function Rerender(path) {
    var conn = new Ext.data.Connection({
        url: "XmlService.ashx?Path=" + path,
        method: "Post"
    });
    Ext.getCmp("grid_view").getStore().proxy.conn = conn;
    Ext.getCmp("grid_view").getStore().reload();
    Ext.getCmp("grid_view").getBottomToolbar().items.item(0).setValue(path);
    document.getElementById("Now_Path").value = path;
}
function GetSelectedDateList(type) {
    var recorders = sm.getSelections();
    if (recorders.length == 0) { Ext.MessageBox.alert("提示信息", "您未选择任何文件"); return; }
    if (SelectDataItems.length != 0) {
        SelectDataItems = new Array();//清空数组
    }
    for (i = 0; i < recorders.length; i++) {
        SelectDataItems.push(recorders[i].data.Name + "☆" + recorders[i].data.Type);
    }

    if (type == "Copy") {
        grid.getTopToolbar().items.item(10).disable();
        grid.getTopToolbar().items.item(16).enable();
        grid.getTopToolbar().items.item(18).enable();
    }
    if (type == "Move") {
        grid.getTopToolbar().items.item(16).disable()
        grid.getTopToolbar().items.item(10).enable();
        grid.getTopToolbar().items.item(18).enable();
    }
    if (type == "Del") {
        grid.getTopToolbar().items.item(10).enable();
        grid.getTopToolbar().items.item(16).enable();
        grid.getTopToolbar().items.item(18).disable();
    }
    if (type == "Zip") {
        grid.getTopToolbar().items.item(10).enable();
        grid.getTopToolbar().items.item(16).enable();
        grid.getTopToolbar().items.item(18).disable();
    }
}

function NewFiles(type) {
    var FileName;
    Ext.Msg.prompt("新建文件", "请输入文件名称", function (btn, text) {
        if (btn == "ok" && text != "") {
            FileName = text;
            Ext.Ajax.request({
                url: "FileManager.ashx",
                method: "Post",
                params: { Type: type, Root: document.getElementById("Now_Path").value, NewName: FileName },
                callback: function (options, success, response) {
                    if (success && response.responseText == "OK") {
                        Ext.MessageBox.alert("提示信息", "成功创建");
                        Rerender(document.getElementById("Now_Path").value);
                    }
                    else {
                        Ext.MessageBox.alert("提示信息", "创建错误(可能是权限不够请联系管理员)");
                    }
                }
            });
        }
    });
}
function Reg_Copy_Files() {
    GetSelectedDateList("Copy");
    fromURL = document.getElementById("Now_Path").value;//保存目前路径
}
function Reg_Move_Files() {
    GetSelectedDateList("Move");
    fromURL = document.getElementById("Now_Path").value;
}
function Reset_RegDatas() {
    SelectDataItems = new Array();
    grid.getTopToolbar().items.item(10).enable();
    grid.getTopToolbar().items.item(16).enable();
    grid.getTopToolbar().items.item(18).disable();
}
function PasteFiles() {
    var type;
    if (SelectDataItems.length > 0) {
        if (grid.getTopToolbar().items.item(10).disabled) {
            Ext.Ajax.request({
                url: "FileManager.ashx",
                method: "Post",
                params: { Path: SelectDataItems, Type: "Copy", Root: document.getElementById("Now_Path").value, FromRoot: fromURL },
                callback: function (options, success, response) {
                    window.alert(response.responseText);
                    window.alert(response.responseText == "OK");
                    if (success && response.responseText == "OK") {
                        Ext.MessageBox.alert("文件操作提示", "拷贝数据成功");
                        Rerender(document.getElementById("Now_Path").value);
                    }
                    else {
                        Ext.MessageBox.alert("文件操作提示", "拷贝数据出错");
                        Rerender(document.getElementById("Now_Path").value);
                    }
                    grid.getTopToolbar().items.item(10).enable();
                    grid.getTopToolbar().items.item(18).enable();
                }
            });
        }
        else {
            Ext.Ajax.request({
                url: "FileManager.ashx",
                method: "Post",
                params: { Path: SelectDataItems, Type: "Move", Root: document.getElementById("Now_Path").value, FromRoot: fromURL },
                callback: function (options, success, response) {
                    window.alert(response.responseText);
                    window.alert(response.responseText == "OK");
                    if (success && response.responseText == "OK") {
                        Ext.MessageBox.alert("文件操作提示", "移动数据成功");
                        Rerender(document.getElementById("Now_Path").value);
                        SelectDataItems = new Array();
                    }
                    else {
                        Ext.MessageBox.alert("文件操作提示", "移动数据出错");
                        Rerender(document.getElementById("Now_Path").value);
                        SelectDataItems = new Array();
                    }
                    grid.getTopToolbar().items.item(16).enable();
                    grid.getTopToolbar().items.item(18).enable();
                }
            });
        }
    }
    else {
        Ext.MessageBox.alert("文件操作提示", "没有可操作的数据（没有选择需要复制或移动的文件）");
    }
}
function DelFiles() {
    GetSelectedDateList("Del");
    if (SelectDataItems.length != 0) {
        Ext.Ajax.request({
            url: "FileManager.ashx",
            method: "Post",
            params: { Path: SelectDataItems, Type: "Del", Root: document.getElementById("Now_Path").value, FromRoot: fromURL },
            callback: function (options, success, response) {
                if (success && response.responseText == "OK") {
                    Ext.MessageBox.alert("文件操作提示", "删除数据成功");
                    Rerender(document.getElementById("Now_Path").value);
                }
                else {
                    Ext.MessageBox.alert("文件操作提示", "删除数据出错");
                    Rerender(document.getElementById("Now_Path").value);
                }
            }
        });
    }
}

function EnterPage(ToUrl) {
    Ext.Ajax.request({
        url: "HistoryManager.ashx",
        method: "Post",
        params: { Path: ToUrl, Type: "enterNew", History: document.getElementById("History_Path").value },
        callback: function (options, success, response) {
            if (success) {
                var res_txt = response.responseText;
                var res_list = res_txt.split("☆");
                document.getElementById("History_Path").value = res_list[1];
                Rerender(ToUrl);
            }
            else { }
        }
    });
}
function DealPage(type) {
    Ext.Ajax.request({
        url: "HistoryManager.ashx",
        method: "Post",
        params: { Type: type, History: document.getElementById("History_Path").value },
        callback: function (options, success, response) {
            if (success) {
                var res_txt = response.responseText;
                var res_list = res_txt.split("☆");
                document.getElementById("History_Path").value = res_list[1];
                if (res_list[0] != "$EmptyError$") {
                    Rerender(res_list[0]);
                }
                else {
                    Ext.MessageBox.alert("提示", "已经没有记录页面");
                }
            }
        }
    });
}

function ZipFiles() {
    GetSelectedDateList("Zip");
    var newName;
    Ext.MessageBox.prompt("压缩", "请输入压缩文件名", function (btn, text) {
        if (btn == "ok" && text != "") {
            Ext.Ajax.request({
                url: "FileManager.ashx",
                method: "Post",
                params: { Type: "Zip", NewName: text, Root: document.getElementById("Now_Path").value, Path: SelectDataItems },
                callback: function (options, success, response) {
                    if (success && response.responseText == "OK") {
                        Ext.MessageBox.alert("提示信息", "压缩成功创建");
                        Rerender(document.getElementById("Now_Path").value);
                    }
                    else {
                        Ext.MessageBox.alert("提示信息", "压缩创建错误(可能是权限不够请联系管理员)");
                    }
                }
            });
        }
    });
}
function UnZipFile() {
    if (sm.getSelected().data.Type == ".zip" || sm.getSelected().data.Type == ".ZIP") {
        Ext.Ajax.request({
            url: "FileManager.ashx",
            method: "Post",
            params: { Type: "UnZip", Root: document.getElementById("Now_Path").value, Path: sm.getSelected().data.FullName },
            callback: function (options, success, response) {
                if (success && response.responseText == "OK") {
                    Ext.MessageBox.alert("提示信息", "成功解压");
                    Rerender(document.getElementById("Now_Path").value);
                }
                else {
                    Ext.MessageBox.alert("提示信息", "解压错误(可能是权限不够请联系管理员)");
                }
            }
        });
    }
    else {
        Ext.Msg.alert("提示", "对不起,选择的文件不是系统可解压的文件");
    }

}

//窗口主函数部分
Ext.onReady(function () {
    Ext.BLANK_IMAGE_URL = "../Content/scripts/extjs/resources/images/default/s.gif";
    grid = new Ext.grid.GridPanel({
        id: "grid_view",
        title: "文件管理",
        width: 800,
        height: 400,
        region: "east",
        sm: sm,
        cm: cm,
        bbar: [
        { xtype: "field", id: 'Now_Path',
            width: 200,
            handler: function () { }
        }, '-', {
            text: "前往",
            handler: function () {
                EnterPage(Ext.getCmp('Now_Path').getValue());
            }
        }],
        tbar: [
            {
                text: "后退",
                handler: function () { DealPage("back"); }
            }, '-', {
                text: "前进",
                handler: function () { DealPage("forward"); }
            }, '-', {
                text: "向上",
                handler: function () { DealPage("up"); }
            }, '-', {
                text: "新建文件",
                handler: function () { NewFiles("NewFile"); }
            }, '-', {
                text: "新建文件夹",
                handler: function () { NewFiles("NewFolder"); }
            }, '-', {
                text: "复制",
                handler: function () { Reg_Copy_Files(); }
            }, '-', {
                text: "粘贴",
                handler: function () { PasteFiles(); }
            }, '-', {
                text: "删除",
                handler: function () { DelFiles(); }
            }, '-', {
                text: "剪切",
                handler: function () { Reg_Move_Files(); }
            }, '-', {
                text: "取消",
                disabled: true,
                handler: function () { Reset_RegDatas(); }
            }, '-', {
                text: "压缩",
                handler: function () { ZipFiles(); }
            }, '-', {
                text: "解压缩",
                handler: function () { UnZipFile(); }
            }
        ],
        viewConfig: {
            forceFit: true
        },
        iconCls: 'icon-grid',
        renderTo: document.body,
        frame: true,
        store: store
    });
    grid.on("rowdblclick", rowdbClick);
    grid.on("rowcontextmenu", open_rowcontextmenu);
    Ext.getCmp('Now_Path').setValue("D:\\");
    store.load();
});